var thrift = require('thrift');
var int64_util = require('thrift/lib/nodejs/lib/thrift/int64_util');
/**
 * Created by Administrator on 2017/3/9.
 */
module.exports = app => {
    class SendController extends app.Controller {
        /**
         * @apiGroup thrift
         * @apiVersion 1.0.0
         * @api {get}/getOrderList 获取订单列表
         * @apiParam {date} starttime 开始时间 
         * @apiParam {date} endtime 结束时间 
         * @apiError (错误情况一) {666} code 输入参数错误
         * @apiSuccessExample {json} 成功返回示例值:
         *     {
         *       
         *     }
         */

        * getOrderList() {
            var starttime = this.ctx.query.starttime;
            var endtime = this.ctx.query.endtime;
            var int64starttime = int64_util.fromDecimalString(starttime);
            var int64endtime = int64_util.fromDecimalString(endtime);
            // var bufstarttime = new Buffer.from(starttime);
            // var bufendtime = new Buffer(endtime);
            // var int64 = new thrift.Int64(1856723764852172).valueOf();
            // console.log(int64);
            // var int64 = new Int64("18567237648521729");
             // console.log(starttime);
             var parameter = {
                
                    starttime: int64starttime.toBuffer(),
                    endtime: int64endtime.toBuffer()
                
            };
        	// var body = this.ctx.body;
        	// const obj = this.ctx.request.body;
        	// var id = this.ctx.helper.mod(obj.orderId, obj.visitId);

        	// keyThrift = yield this.service.hyperbahnclient.insert(0);
            console.log(parameter);
            // console.log(JSON.parse(parameter.toString());
            // console.log(JOSN.stringify(parameter.toString()));
            
       //  	var parameter = {
			    // starttime: bufstarttime,
       //          endtime: bufendtime
       //      };
            var resp  = yield this.app.requestThriftChannel('process_coreorder_service','CoreOrderServer::getOrderBeanList',parameter);// , parameter);
            // var ret = {};
            // resp.body.forEach((obj) => {
            //     var o1 = new thrift.Int64(obj.orderid);
            //     var o2 = new thrift.Int64(obj.visitid);
            //     ret.orderid = int64_util.toDecimalString(o1);
            //     ret.visitid = int64_util.toDecimalString(o2);
            //     ret.starttime = obj.starttime.toString();
            //     ret.endtime = obj.endtime.toString();
            // });
                         this.ctx.body = resp;
        }
        /**
         * @apiGroup thrift
         * @apiVersion 1.0.0
         * @api {post}/insertOrder 插入订单
         * @apiParam {int} orderId 订单号 
         * @apiParam {int} visitId  
         * @apiSuccessExample {json} 成功返回示例值:
         *     {
         *       
         *     }
         */
        * insertOrder() {
            var body = this.ctx.body;
            const obj = this.ctx.request.body;
            console.log(body);console.log(obj);
            // var id = this.ctx.helper.mod(obj.orderId, obj.visitId);

            // keyThrift = yield this.service.hyperbahnclient.insert(0);
            var int64orderId = int64_util.fromDecimalString(obj.orderId);
            var int64visitId = int64_util.fromDecimalString(obj.visitId);
            var parameter = {
                orderbean: {
                    orderid: int64orderId.toBuffer(),
                    visitid: int64visitId.toBuffer()
                }
            };
            var resp  = yield this.app.requestThriftChannel('process_coreorder_service','CoreOrderServer::insertOrder',parameter);// , parameter);
            this.ctx.body = resp;
        }
        /**
         * @apiGroup thrift
         * @apiVersion 1.0.0
         * @api {get}/getOrderById/:id 根据id获取订单
         * @apiSuccessExample {json} 成功返回示例值:
         *     {
         *       
         *     }
         */
        * getOrderById() {
            // const obj = this.ctx.params.id;
            
            var int64 = int64_util.fromDecimalString(this.ctx.params.id);

            // var l = new Int64(parseInt('1856723764852172', 10))
            // var orderId = new Int64(18567237648521729); // this.ctx.params.id);
            // console.log(parseInt('1856723764852172', 10));
            // console.log(typeof(orderId));
            // var id = this.ctx.helper.mod(obj.orderId, obj.visitId);
            // var nt = new Int64(this.ctx.params.id);
            // console.log(nt.toBuffer());
              // console.log(int.valueOf());
               // int.toBuffer().toString('hex'),
            // keyThrift = yield this.service.hyperbahnclient.insert(0);
            var parameter = {
                orderid: int64.toBuffer()
            };
            var resp  = yield this.app.requestThriftChannel('process_coreorder_service','CoreOrderServer::getOrderBeanById',parameter);// , parameter);
            this.ctx.body = resp;
        }
        /**
         * @apiGroup thrift
         * @apiVersion 1.0.0
         * @api {post}/postfile 发送文件
         * @apiParam {string} file 文件路径 e://input.txt
         * @apiSuccessExample {json} 成功返回示例值:
         *     {
         *       
         *     }
         */
        * postfile() {
            const obj = this.ctx.request.body;
            var rf=require("fs");  
            var data=rf.readFileSync(obj.file);
            var buf = new Buffer(data);
            var parameter = {
                file: buf
            };
            var resp  = yield this.app.requestThriftChannel('process_coreorder_service','CoreOrderServer::postfile',parameter);// , parameter);
            console.log(resp);
              this.ctx.body = resp;
            
        }

        /**
         * @apiGroup thrift
         * @apiVersion 1.0.0
         * @api {delete}/deleteOrder/:id 删除订单
         * @apiSuccessExample {json} 成功返回示例值:
         *     {
         *       
         *     }
         */
        * deleteOrder() {
             var orderId = this.ctx.params.id;
             console.log(orderId);
           let int64 = int64_util.fromDecimalString(this.ctx.params.id);
            // var id = this.ctx.helper.mod(obj.orderId, obj.visitId);

            // keyThrift = yield this.service.hyperbahnclient.insert(0);
            var parameter = {
                orderid: int64.toBuffer()
            };
            var resp  = yield this.app.requestThriftChannel('process_coreorder_service','CoreOrderServer::deleteOrder',parameter);// , parameter);
            this.ctx.body = resp;
        }
  //       const getRepoData = (options) => {
		//   return new Promise((resolve, reject) => {
		//     request(options, (err, res, body) => {
		//       if (err) {
		//         reject(err);
		//       }
		//       resolve(body);
		//     });
		//   });
		// };
        async selectdb () {
        	var obj = await new Promise((resolve, reject) => {
        		this.ctx.helper.getdb().get('SELECT * FROM Note;', (err,row) => {
        			if(err)
        				reject(err);
        			resolve(row);
        		});
        	});
        	
        	console.log(obj);
        	// obj.then((err, row) => {console.log(row);});
        	this.ctx.body = obj;
        }
        async conndb () {
        	var obj = await this.ctx.helper.conn();
        	
        	console.log(obj);
        	// obj.then((err, row) => {console.log(row);});
        	this.ctx.body = obj;
        }
    }
    return SendController;
};

